package com.ycy.leetcode.offer;

import java.util.ArrayList;
import java.util.List;

public class Offer5 {

  public static void main(String[] args) {
    String s = replaceSpace("We are h");
    System.out.println(s);
  }

  public static String replaceSpace(String s) {
    int konglength = 0;
    for (int i = 0; i < s.length(); i++) {
      char ix = s.charAt(i);
      if (' ' == ix) {
        konglength++;
      }
    }
    // 扩容
    char[] news = new char[s.length() + 2 * konglength];
    int k = news.length - 1;

    for (int i = s.length() - 1; i >= 0; i--) {
//      if (i == k) {
//        break;
//      }
      char ix = s.charAt(i);
      if (' ' == ix) {
        news[k] = '0';
        news[--k] = '2';
        news[--k] = '%';
      } else {
        news[k] = ix;
      }
      k--;

    }
    return new String(news);

  }

}
